using System;
using System.Windows.Forms;
using SpellCenter.Core;
using SpellCore;

namespace SpellCenter.UI
{
    public class SpellStatusValueColumn : DataGridViewColumn
    {
        public SpellStatusValueColumn()
            : base(new SpellStatusValueCell())
        {
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                // Ensure that the cell used for the template is a SpellStatusValueCell.
                if (value != null &&
                    !value.GetType().IsAssignableFrom(typeof(SpellStatusValueCell)))
                {
                    throw new InvalidCastException("Must be a SpellStatusValueCell");
                }
                base.CellTemplate = value;
            }
        }
    }

    public class SpellStatusValueCell : DataGridViewTextBoxCell
    {

        public SpellStatusValueCell()
        {
            Initialize(SpellWordInfo.CheckingResultToString(CheckingResult.Unknown));
        }
        public SpellStatusValueCell(string status)
        {
            Initialize(status);
        }

        private void Initialize(string status)
        {
            Value = status;
            Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
        }

//        protected override void OnMouseEnter(int rowIndex)
//        {
//            base.OnMouseEnter(rowIndex);
//            Selected = true;
//            DataGridView.Rows[rowIndex].Selected = true;
//            DataGridView.BeginEdit(false);
//        }

        #region DataGridViewButtonCell Members

        /// <summary>
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <returns>The CheckingResult value of the Cell.  If out of range, then default value.</returns>
        protected override object GetValue(int rowIndex)
        {
            SpellGridControl gridControl = (SpellGridControl)DataGridView.Parent;
            if(rowIndex < 0 || rowIndex >= gridControl.Model.Data.Count)
            {
                return DefaultNewRowValue;
            }
            else return gridControl.Model.Data[rowIndex].StatusString;
        }

        public override Type ValueType
        {
            get
            {
                return typeof(string);
            }
        }

        public override Type FormattedValueType
        {
            get
            {
                return typeof(string);
            }
        }

        public override object DefaultNewRowValue
        {
            get
            {
                return SpellWordInfo.CheckingResultToString(CheckingResult.Unknown);
            }
        }

        #endregion

    }
}